<template>
  <el-dialog
    :title="!dataForm.uid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="社区名称" prop="communityName">
      <el-input v-model="dataForm.communityName" placeholder="社区名称"></el-input>
    </el-form-item>
    <el-form-item label="所属榜单uid" prop="rankUid">
      <el-input v-model="dataForm.rankUid" placeholder="所属榜单uid"></el-input>
    </el-form-item>
    <el-form-item label="所属榜单名称" prop="rankName">
      <el-input v-model="dataForm.rankName" placeholder="所属榜单名称"></el-input>
    </el-form-item>
    <el-form-item label="社区描述" prop="descript">
      <el-input v-model="dataForm.descript" placeholder="社区描述"></el-input>
    </el-form-item>
    <el-form-item label="社区logo地址" prop="logo">
      <el-input v-model="dataForm.logo" placeholder="社区logo地址"></el-input>
    </el-form-item>
    <el-form-item label="检索首字母" prop="firstLetter">
      <el-input v-model="dataForm.firstLetter" placeholder="检索首字母"></el-input>
    </el-form-item>
    <el-form-item label="顺序" prop="sort">
      <el-input v-model="dataForm.sort" placeholder="顺序"></el-input>
    </el-form-item>
    <el-form-item label="状态：0删除的 1激活的 2冻结的 3置顶的" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态：0删除的 1激活的 2冻结的 3置顶的"></el-input>
    </el-form-item>
    <el-form-item label="创建人uid" prop="createUserUid">
      <el-input v-model="dataForm.createUserUid" placeholder="创建人uid"></el-input>
    </el-form-item>
    <el-form-item label="创建人名称" prop="createUserName">
      <el-input v-model="dataForm.createUserName" placeholder="创建人名称"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          uid: 0,
          communityName: '',
          rankUid: '',
          rankName: '',
          descript: '',
          logo: '',
          firstLetter: '',
          sort: '',
          status: '',
          createUserUid: '',
          createUserName: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          communityName: [
            { required: true, message: '社区名称不能为空', trigger: 'blur' }
          ],
          rankUid: [
            { required: true, message: '所属榜单uid不能为空', trigger: 'blur' }
          ],
          rankName: [
            { required: true, message: '所属榜单名称不能为空', trigger: 'blur' }
          ],
          descript: [
            { required: true, message: '社区描述不能为空', trigger: 'blur' }
          ],
          logo: [
            { required: true, message: '社区logo地址不能为空', trigger: 'blur' }
          ],
          firstLetter: [
            { required: true, message: '检索首字母不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '顺序不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态：0删除的 1激活的 2冻结的 3置顶的不能为空', trigger: 'blur' }
          ],
          createUserUid: [
            { required: true, message: '创建人uid不能为空', trigger: 'blur' }
          ],
          createUserName: [
            { required: true, message: '创建人名称不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.uid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.uid) {
            this.$http({
              url: this.$http.adornUrl(`/community/community/info/${this.dataForm.uid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.communityName = data.community.communityName
                this.dataForm.rankUid = data.community.rankUid
                this.dataForm.rankName = data.community.rankName
                this.dataForm.descript = data.community.descript
                this.dataForm.logo = data.community.logo
                this.dataForm.firstLetter = data.community.firstLetter
                this.dataForm.sort = data.community.sort
                this.dataForm.status = data.community.status
                this.dataForm.createUserUid = data.community.createUserUid
                this.dataForm.createUserName = data.community.createUserName
                this.dataForm.createTime = data.community.createTime
                this.dataForm.updateTime = data.community.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/community/community/${!this.dataForm.uid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'uid': this.dataForm.uid || undefined,
                'communityName': this.dataForm.communityName,
                'rankUid': this.dataForm.rankUid,
                'rankName': this.dataForm.rankName,
                'descript': this.dataForm.descript,
                'logo': this.dataForm.logo,
                'firstLetter': this.dataForm.firstLetter,
                'sort': this.dataForm.sort,
                'status': this.dataForm.status,
                'createUserUid': this.dataForm.createUserUid,
                'createUserName': this.dataForm.createUserName,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
